﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Core.Repositories;
using Core.Models;

namespace BlogMvc.Controllers
{
    public class CommentController : Controller
    {
        CommentRepository _storeDb;

        public CommentController()
        {
            _storeDb = new CommentRepository();
        }

        #region ver_0.1 Code first
        ////
        //// GET: /Comment/ViewComments
        //public PartialViewResult ViewComments(int postId, bool isAuthorized = false)
        //{
        //    ViewBag.Authorized = isAuthorized;
        //    var comments = _storeDb.GetByPostId(postId);
        //    return PartialView(comments.ToList());
        //}

        ////
        //// AJAX: /Comment/Delete
        //[HttpPost]
        //public ActionResult Delete(int id)
        //{
        //    var comment = _storeDb.GetById(id);
        //    if (comment != null)
        //    {
        //        _storeDb.Delete(id);
        //    }

        //    return RedirectToAction("ViewComments", new { postId = comment.PostId, isAdmin = true });
        //}

        ////
        //// AJAX: /Comment/Create        
        //public ActionResult Create(int postId, Comment comment)
        //{
        //    if (comment == null)
        //    {
        //        comment = new Comment();
        //    }
        //    comment.PostId = postId;

        //    return View(comment);        
        //}

        //[HttpPost]
        //public ActionResult Create(Comment newComment)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        _storeDb.Create(newComment);               

        //        return RedirectToAction("Create", "Comment", new { postId = newComment.PostId, comment = new Comment() });
        //    }
        //    else
        //    {
        //        return View(newComment);
        //    }
        //}
        #endregion

        #region ver_0.2 Database First
        //
        // GET: /Comment/ViewComments
        public PartialViewResult ViewComments(int postId, bool isAuthorized = false)
        {
            ViewBag.Authorized = isAuthorized;
            var comments = _storeDb.GetMany(t => t.PostId == postId);
            return PartialView(comments.ToList());
        }

        //
        // AJAX: /Comment/Delete
        [HttpPost]
        [Authorize]
        public ActionResult Delete(int id)
        {
            var comment = _storeDb.GetOne(t => t.Id == id);
            int postId = comment.PostId != null ? (int)comment.PostId : 0;
            if (comment != null)
            {
                _storeDb.Delete(comment);
            }

            return RedirectToAction("ViewComments", new { postId = postId, isAuthorized = true });
        }

        //
        // AJAX: /Comment/Create        
        public ActionResult Create(int postId, Comment comment)
        {
            if (comment == null)
            {
                comment = new Comment();
            }
            comment.PostId = postId;

            return View(comment);
        }

        [HttpPost]
        public ActionResult Create(Comment newComment)
        {
            if (ModelState.IsValid)
            {
                _storeDb.Create(newComment);

                return RedirectToAction("Create", "Comment", new { postId = newComment.PostId, comment = new Comment() });
            }
            else
            {
                return View(newComment);
            }
        }
        #endregion
    }
}
